<?php
/**
 * Fastpay
 * 只接入代付, 暂时只支持一个Fastpay渠道账号.
 */

namespace app\api\service\payment;

use app\api\service\ApiPayment;

class Fastpay extends ApiPayment
{
    const API_URL = 'http://fastpay.785f7.com/';

    public function checkOrderTask()
    {
        $list = $this->modelBalanceCash->where('status', BalanceCashEnum::PayAppliy)
            ->limit(10)
            ->order('id', 'desc')
            ->select();
        foreach ($list as $item) {
        }
    }

    /**
     * 代付.
     */
    public function pay($tradeNo, $name, $bank, $amount, $bankCode, $idcardNo, $mobile)
    {
        $params = [
            'out_trade_no' => $tradeNo,   //下游代付单号
            'account_name' => $name, //收款人姓名
            'bank_card_no' => $bank, //银行卡号
            'amount' => $amount, //金额(元)
            'idcard_no' => $idcardNo,  //收款人身份证
            'mobile' => $mobile,    //手机
            'bank_code' => $bankCode,    //代付银行编码
            'channel_code' => $this->config['channel_code'],
            'pay_type'      => 'payment',  //接口类型
            // 'mermo'         => '',   //代付摘要
        ];

        return $this->requestApi('payment/pay.do', $params);
    }

    /**
     * 查询订单信息.
     */
    public function orderQuery($tradeNo)
    {
        $params = [
            'out_trade_no' => $tradeNo,   //下游代付单号
        ];

        return $this->requestApi('payment/orderQuery.do', $params);
    }

    /**
     * 请求接口.
     *
     * @param string $method
     * @param array  $params
     *
     * @return array
     */
    public function requestApi($method, $params)
    {
        $params['store_no'] = $this->config['app_id'];
        $params['sign'] = $this->buildRequestSign($params, $this->config['private_key']);
        $result = self::curl(self::API_URL.$method, json_encode($params));

        return json_decode($result, true);
    }

    public static function curl($url, $data)
    {
        header('Content-type: text/html; charset=utf-8');
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        // 告诉curl禁用ssl当中的公用名,公用名只有两种值0表没有，1表示拥有，默认为1
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        //为没有ssl所有不要进行认证的校验
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt(
            $curl,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: '.strlen($data),
            )
        );

        //执行命令
        $str = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //把请求的数据进行返回
        return $str;
    }

    /**
     * 签名.
     *
     * @param $params
     * @param $key
     *
     * @return string
     */
    private function buildRequestSign($params, $key)
    {
        return md5(self::getSignContent($params).'key='.$key);
    }

    /**
     * 数据排序.
     *
     * @param $params
     *
     * @return string
     */
    private function getSignContent($params)
    {
        ksort($params);

        $stringToBeSigned = '';
        foreach ($params as $k => $v) {
            $stringToBeSigned .= $k.'='.$v.'&';
        }

        return $stringToBeSigned;
    }

    /**
     * 回调成功参数
     */
    public function callbackSuccessResponse()
    {
        return "ok";
    }
}
